﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ResourceEditor.Core.Search;


namespace Editor
{
    public partial class Find : Form
    {
        #region Properties
        private SearchStructure Structure { get; set; }

        private bool IsValid
        {
            get
            {
                bool result = true;

                if (txtFind.Text.Trim() == string.Empty)
                {
                    result = false;
                    errorProvider1.SetError(txtFind, "Cannot be empty");
                }
                else
                {
                    errorProvider1.SetError(txtFind, string.Empty);
                }

                if (!chkKeys.Checked && !chkValues.Checked)
                {
                    result = false;
                    errorProvider1.SetError(chkValues, "Please mark Keys or Values");
                }
                else
                {
                    errorProvider1.SetError(chkValues, string.Empty);
                }


                return result;
            }
        }
        #endregion

        #region Define Events
        internal event EventHandler<SearchStructureEventArgs> Next;
        #endregion

        #region Ctor
        public Find()
        {
            InitializeComponent();
            Structure = new SearchStructure();
        }
        #endregion

        #region Methods
        public void Show(string current)
        {
            cmbLook.Items.Clear();

            if (Main.Manager != null)
            {
                cmbLook.Items.AddRange(Main.Manager.ResoueceNames);

                if (current != string.Empty)
                {
                    cmbLook.SelectedItem = current;
                }
                else
                {
                    cmbLook.SelectedIndex = 0;
                }
            }

            btnNext.Enabled = cmbLook.Items.Count > 0;
            base.Show();
        }

        public new void Show()
        {
            Show(string.Empty);
        }

        private void OnNext()
        {
            if (Next != null)
            {
                Next(this, new SearchStructureEventArgs(Structure));
            }
        }

        private void CreateStructure()
        {
            Structure.Contain = chkContain.Checked;
            Structure.FindWhat = txtFind.Text;
            Structure.Keys = chkKeys.Checked;
            Structure.LookIn = cmbLook.Text;
            Structure.Values = chkValues.Checked;
        }
        #endregion

        #region Evenrs
        private void Find_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                CreateStructure();
                OnNext();
            }
        }
        #endregion
    }

    internal class SearchStructureEventArgs : EventArgs
    {
        internal SearchStructure Structure { get; private set; }

        internal SearchStructureEventArgs(SearchStructure structure)
        {
            Structure = structure;
        }
    }
}
